package com.zcn.io.web.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.zcn.io.web.api.dto.DictDto;
import com.zcn.io.web.auth.LoginUser;
import com.zcn.io.web.mapper.AgencyMapper;
import com.zcn.io.common.entity.Agency;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @desc:
 * @author: 202438 邱建伟
 * @date: 2024/9/20 22:42
 * @Copyright (c) 2024, 江苏金恒信息科技股份有限公司
 */
@Service
public class AgencyService {

    @Autowired
    private AgencyMapper agencyMapper;


    public List<Agency> list(Agency agency) {
        //从springSecurity中获取权限信息
        LambdaQueryChainWrapper<Agency> lambdaQueryWrapper = new LambdaQueryChainWrapper<>(agencyMapper);
        if (StrUtil.isNotEmpty(agency.getIndexId())){
            lambdaQueryWrapper.like(Agency::getIndexId, agency.getIndexId());
        }
        if (StrUtil.isNotEmpty(agency.getAgencyName())){
            lambdaQueryWrapper.like(Agency::getAgencyName, agency.getAgencyName());
        }
        lambdaQueryWrapper.last("ORDER BY index_id + 0");
        return lambdaQueryWrapper.select().list();
    }

    public void save(Agency agency) {
        this.agencyMapper.insert(agency);
    }

    public Agency getById(String id) {
        return this.agencyMapper.selectById(id);
    }

    public void updateById(Agency agency) {
        this.agencyMapper.updateById(agency);
    }

    public void deleteById(String id) {
        this.agencyMapper.deleteById(id);
    }

    public List<DictDto> dict() {
        return this.agencyMapper.dict();
    }
}
